/**
 * 
 */
package com.ideeli.webextractor.service.old;

import java.util.NoSuchElementException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import com.ideeli.webextractor.domain.ProductDetail;
import com.ideeli.webextractor.utils.Utility;

/**
 * @author sa
 *
 */
public class ExtractProdDetailsService
{
	
	private String largeBasePath="S:\\webextractor\\data\\images\\large\\";
	private String smallBasePath="S:\\webextractor\\data\\images\\small\\";
	
	public ProductDetail extract(WebDriver driver) throws Exception
	{
		ProductDetail prodDetail= new ProductDetail();
		
		try{
			
			WebElement productNameWE = driver.findElement(By.className("product-name"));
			System.out.println("productNameWE  : " + productNameWE.getText());
			prodDetail.setProductName(productNameWE.getText());
			
			WebElement vendorWE = driver.findElement(By.className("vendor"));
			System.out.println("vendorWE  : " + vendorWE.getText());
			prodDetail.setVendor(vendorWE.getText().trim());

			WebElement categoryTableWE = driver.findElement(By.className("category-table"));
			System.out.println("categoryTableWE  : " + categoryTableWE.getText());
			Utility.parseCategory(categoryTableWE.getText(), prodDetail);
			
			WebElement yourPriceWE = driver.findElement(By.className("your-price"));
			System.out.println("yourPriceWE  : " + yourPriceWE.getText());
			prodDetail.setYourPrice(Utility.extractPrice(yourPriceWE.getText()));

			WebElement inStockWE = driver.findElement(By.className("in-stock"));
			System.out.println("inStockWE  : " + inStockWE.getText());
			prodDetail.setInStock("In Stock".equals(inStockWE.getText().trim())? true : false);

			WebElement infoCodesWE = driver.findElement(By.className("info-codes"));
			System.out.println("infoCodesWE  : " + infoCodesWE.getText());
			Utility.parseInfoCodesWE(infoCodesWE.getText(), prodDetail);
			

			WebElement productInformationWE = driver.findElement(By.id("product-information"));
			System.out.println("productInformationWE  : " + productInformationWE.getText());
			prodDetail.setProductInformation(productInformationWE.getText());
			Utility.parseBasicProdInformation(productInformationWE.getText(), prodDetail);
			
			String innerHTML=productInformationWE.getAttribute("innerHTML");
			System.out.println("innerhtml");
			System.out.println(innerHTML);
		
			try
			{
				WebElement extendedSpecSelWE = driver.findElement(By.id("selector-extended-specifications"));
				extendedSpecSelWE.click();
				System.out.println("extendedSpecSelWE  : " + extendedSpecSelWE.getText());//NoText just for clicking purpose
			}
			catch(Throwable nse)
			{
				System.out.println("selector-extended-specifications does not exist ! skip");
				
			}

			try
			{
				WebElement extendedSpecWE = driver.findElement(By.id("extended-specifications"));
				System.out.println("extendedSpecWE  : " + extendedSpecWE.getText());
				prodDetail.setExtendedSpec(extendedSpecWE.getText());
				Utility.parseExtendedProdInformation(extendedSpecWE.getText(), prodDetail);
				innerHTML=extendedSpecWE.getAttribute("innerHTML");
				System.out.println("innerhtml");
				System.out.println(innerHTML);
				
			}
			catch(Throwable nse)
			{
				System.out.println("extended-specifications  does not exist ! skip");
			}

			

			WebElement prodImageWE = driver.findElement(By.id("ctl00_PlaceHolderMain_pdTopControl_uc_ProductDetailsImageControl_lnk_ImageLink"));
			System.out.println("prodImageWE  : " + prodImageWE.getText());
			
			String srcLink=prodImageWE.getAttribute("href");
			System.out.println("srcLink  : " + srcLink);
			String imgFleName=prodDetail.getSku()+".jpg";
			String sImgFleName=prodDetail.getSku()+"-95.jpg";
			//Devie fileName from here.
//			if(srcLink!=null)
//			{
//				Utility.saveImage(srcLink, largeBasePath+imgFleName);
//				srcLink=srcLink.replaceAll("300", "95");
//				System.out.println("srcLink  : " + srcLink);
//				Utility.saveImage(srcLink, smallBasePath+sImgFleName);
//			}
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException("element not found:",e);
		}

		
		return prodDetail;
	}
	

}
